package okhttp.itydl.com.download.utils;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * @author howard
 */
public class Md5Utils {
    /**
     * URL进行MD5加密
     * @param url
     * @return
     */
    public static String generateCode(String url){
        if(TextUtils.isEmpty(url)){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            digest.update(url.getBytes());
            byte[] caper = digest.digest();
            for (byte b : caper) {
                String hexString = Integer.toHexString(b & 0xff);
                sb.append(hexString.length() == 1 ? "0"+hexString : hexString);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
